package com.qf.shiro;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.qf.mapper.DtsAdminMapper;
import com.qf.mapper.DtsPermissionMapper;
import com.qf.mapper.DtsRoleMapper;
import com.qf.pojo.DtsAd;
import com.qf.pojo.DtsAdmin;
import com.qf.pojo.DtsPermission;
import com.qf.pojo.DtsRole;
import com.qf.service.DtsPermissionService;
import com.qf.service.DtsRoleService;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Set;
import java.util.stream.Collector;
import java.util.stream.Collectors;


public class AdminAuthorizingRealm extends AuthorizingRealm {

    @Autowired
    private DtsAdminMapper dtsAdminMapper;

    @Autowired
    private DtsRoleService dtsRoleService;
    @Autowired
    private DtsPermissionService dtsPermissionService;


    /**
     * 授权   AuthorizationInfo
     * @param principals
     * @return
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        DtsAdmin dtsAdmin = (DtsAdmin) principals.getPrimaryPrincipal();
        SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();

        //1.查询角色 Set<String> roles
        //String roleIds = "[1,2]" Mybatis 类型转换器 select * role_ids from dts_admin where ...
        Integer[] roleIds = dtsAdmin.getRoleIds();
        Set<String> roles = dtsRoleService.findRolesByIds(roleIds);
        System.out.println("当前用户的:"+roles);
        simpleAuthorizationInfo.addRoles(roles);
        //2.查询权限 Set<String> perms;
        Set<String> dtsPermissions = dtsPermissionService.findPermsByRoleIds(roleIds);
        simpleAuthorizationInfo.addStringPermissions(dtsPermissions);
        System.out.println("当前用户的:"+dtsPermissions);
        return simpleAuthorizationInfo;
    }

    /**
     * 认证   AuthenticationInfo
     * @param token
     * @return
     * @throws AuthenticationException
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        UsernamePasswordToken usernamePasswordToken=(UsernamePasswordToken) token;
        String username = usernamePasswordToken.getUsername();
        String  password=new String(usernamePasswordToken.getPassword());

        //管理员对象
        DtsAdmin dtsAdmin = dtsAdminMapper.selectOne(new LambdaQueryWrapper<DtsAdmin>()
                .eq(DtsAdmin::getUsername,username)
                .eq(DtsAdmin::getDeleted,false));
        if (null == dtsAdmin){
            throw  new UnknownAccountException();
        }
        if (!dtsAdmin.getPassword().equals(password)){
            throw new IncorrectCredentialsException();//密码不正确
        }
        SimpleAuthenticationInfo simpleAuthenticationInfo = new SimpleAuthenticationInfo(dtsAdmin,password,this.getName());
        return simpleAuthenticationInfo;
    }
}
